package es.udc.emarketapp.http.view.actionForms;

import java.util.Collection;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;

import es.udc.emarketapp.emarket.model.country.vo.CountryVO;
import es.udc.emarketapp.emarket.model.countryFacade.CountryFacade;
import es.udc.emarketapp.http.util.struts.action.EMarketPropertyValidator;
import es.udc.emarketapp.http.view.applicationObjects.DateRanges;
import es.udc.fbellas.j2ee.util.struts.action.PropertyValidator;
import es.udc.fbellas.j2ee.util.struts.action.DefaultActionForm;
import java.util.Calendar;
import org.apache.struts.action.ActionMessage;

public class RegisterSellerForm extends DefaultActionForm {

    public final static String REGISTERSELLER_ACTION = "REGISTERSELLER";
    public final static String UPDATESELLER_ACTION = "UPDATESELLER";
    public final static String CHANGE_PASS_ACTION = "CHANGEPASS";
    public final static String GOBACK = "GOBACK";
    private final static Collection<String> ACTION_TYPES = Arrays.asList(
            new String[]{CHANGE_PASS_ACTION, REGISTERSELLER_ACTION,
                GOBACK, UPDATESELLER_ACTION});
    private String idSeller;
    private String pass;
    private String retypePass;
    private String oldPassword;
    private String name;
    private String surname1;
    private String surname2;
    private String nifCif;
    private String address;
    private String city;
    private String cp;
    private String province;
    private String country;
    private String email;
    private String tlf1;
    private String tlf2;
    private String nomEmp;
    private String nRegMerc;
    private String fax;
    private String iban;
    private String swift;
    private String action;
    private String back;

    public RegisterSellerForm() {
        reset();
    }

    public String getAction() {
        return action;
    }

    public void setAction(String action) {
        this.action = action;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    
    public String getBack() {
        return back;
    }

    public void setBack(String back) {
        this.back = GOBACK;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getCp() {
        return cp;
    }

    public void setCp(String cp) {
        this.cp = cp;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getNifCif() {
        return nifCif;
    }

    public void setNifCif(String nifCif) {
        this.nifCif = nifCif;
    }

    public String getOldPassword() {
        return oldPassword;
    }

    public void setOldPassword(String oldPassword) {
        this.oldPassword = oldPassword;
    }

    public String getPass() {
        return pass;
    }

    public void setPass(String pass) {
        this.pass = pass;
    }

    public String getProvince() {
        return province;
    }

    public void setProvince(String province) {
        this.province = province;
    }

    public String getRetypePass() {
        return retypePass;
    }

    public void setRetypePass(String retypePass) {
        this.retypePass = retypePass;
    }

    public String getSurname1() {
        return surname1;
    }

    public void setSurname1(String surname1) {
        this.surname1 = surname1;
    }

    public String getSurname2() {
        return surname2;
    }

    public void setSurname2(String surname2) {
        this.surname2 = surname2;
    }

    public String getTlf1() {
        return tlf1;
    }

    public void setTlf1(String tlf1) {
        this.tlf1 = tlf1;
    }

    public String getTlf2() {
        return tlf2;
    }

    public void setTlf2(String tlf2) {
        this.tlf2 = tlf2;
    }
    
    public String getIdSeller() {
		return idSeller;
	}

	public void setIdSeller(String idSeller) {
		this.idSeller = idSeller;
	}

	public String getNomEmp() {
		return nomEmp;
	}

	public void setNomEmp(String nomEmp) {
		this.nomEmp = nomEmp;
	}

	public String getnRegMerc() {
		return nRegMerc;
	}

	public void setnRegMerc(String nRegMerc) {
		this.nRegMerc = nRegMerc;
	}

	public String getFax() {
		return fax;
	}

	public void setFax(String fax) {
		this.fax = fax;
	}

	public String getIban() {
		return iban;
	}

	public void setIban(String iban) {
		this.iban = iban;
	}

	public String getSwift() {
		return swift;
	}

	public void setSwift(String swift) {
		this.swift = swift;
	}

	public ActionErrors validate(ActionMapping mapping,
            HttpServletRequest request) {

        ActionErrors errors = new ActionErrors();
        PropertyValidator.validateString(errors, "action", action, true,
                ACTION_TYPES);
//        System.out.println("action  = " + action);
        if (!(GOBACK.equals(back))) {
            if (REGISTERSELLER_ACTION.equals(action)
                    || UPDATESELLER_ACTION.equals(action)) {
                PropertyValidator.validateMandatory(errors, "name", name);
                PropertyValidator.validateMandatory(errors, "surname1",
                        surname1);
                EMarketPropertyValidator.validateString(errors, "nifCif",
                        nifCif, 9, true);
                PropertyValidator.validateMandatory(errors, "email", email);
                PropertyValidator.validateMandatory(errors, "address",
                        address);
                PropertyValidator.validateMandatory(errors, "city", city);
                PropertyValidator.validateMandatory(errors, "cp", cp);
                PropertyValidator.validateMandatory(errors, "province",
                        province);
                PropertyValidator.validateMandatory(errors, "country", country);
                PropertyValidator.validateMandatory(errors, "tlf1", tlf1);
     

                if (email != null && email.compareTo("") != 0) {
                    EMarketPropertyValidator.validateEmailAddress(errors,
                            "email", email);
                }

                if (REGISTERSELLER_ACTION.equals(action)) {
                    EMarketPropertyValidator.validateString(errors, "idSeller",
                            idSeller, 8, true);
                    EMarketPropertyValidator.validateString(errors, "pass", pass,
                            8, true);
                    EMarketPropertyValidator.validateString(errors, "retypePass",
                            retypePass, 8, true);

                    if (pass != null) {
                        if (retypePass != null) {
                            if (!pass.equals(retypePass)) {
                                errors.add("retypePass",
                                        new ActionMessage("ErrorMessages.password.doNotMatch"));
                            }
                        } else {
                            errors.add("retypePass",
                                    new ActionMessage("ErrorMessages.password.doNotMatch"));
                        }
                    } else {
                        if (retypePass != null) {
                            errors.add("retypePass",
                                    new ActionMessage("ErrorMessages.password.doNotMatch"));
                        }
                    }
                }

            } else if (CHANGE_PASS_ACTION.equals(action)) {

                System.out.println(oldPassword);
                if (oldPassword != null) {
                    if (oldPassword == null || oldPassword.compareToIgnoreCase("") == 0) {
                        errors.add("oldPassword",
                                new ActionMessage("ErrorMessages.mandatoryField"));
                    } else {
                        System.out.println(this.oldPassword);
                    }
                    if (retypePass != null) {
                        if (!retypePass.equals(retypePass)) {
                            errors.add("retypePass",
                                    new ActionMessage("ErrorMessages.password.doNotMatch"));
                        }
                    } else {
                        errors.add("retypePass",
                                new ActionMessage("ErrorMessages.password.doNotMatch"));
                    }
                } else {
                    if (retypePass != null) {
                        errors.add("retypePass",
                                new ActionMessage("ErrorMessages.password.doNotMatch"));
                        if (oldPassword == null || oldPassword.compareToIgnoreCase("") == 0) {
                            errors.add("oldPassword",
                                    new ActionMessage("ErrorMessages.mandatoryField"));
                        } else {
                            System.out.println(this.oldPassword);
                        }

                    }
                }
            }


        }

        /*
        if (!errors.isEmpty()) {
          
            try {
                CountryFacade countryFacade = new CountryFacade();
                List<CountryVO> countryVOs = countryFacade.findAllCountries();
                request.setAttribute("countries", countryVOs);
            } catch (Exception e) {
            	System.out.print("error al cargar el listado de paises");
            }

        }
         * 
         */

        return errors;

    }

    private void reset() {
        idSeller = null;
        pass = null;
        retypePass = null;
        name = null;
        surname1 = null;
        surname2 = null;
        nifCif = null;
        address = null;
        city = null;
        cp = null;
        province = null;
        country = null;
        email = null;
        tlf1 = null;
        tlf2 = null;
        nomEmp = null;
        nRegMerc = null;
        fax = null;
        iban = null;
        swift = null;
    }
}
